/******************************************************************************
 * 
 * Announce: CSharpKit, Basic algorithms, components and definitions.
 *           Copyright (C) ShenYongchen.
 *           All rights reserved.
 *   Author: 申永辰.郑州 (shenyczz@163.com)
 *  WebSite: http://gitee.com/shenyczz/csharp_csk
 *
 * THIS CODE IS LICENSED UNDER THE MIT LICENSE (MIT).
 * THIS CODE IS PROVIDED *AS IS* WITHOUT WARRANTY OF 
 * ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING ANY
 * IMPLIED WARRANTIES OF FITNESS FOR A PARTICULAR
 * PURPOSE, MERCHANTABILITY, OR NON-INFRINGEMENT.
 * 
******************************************************************************/

using System;
using System.Runtime.InteropServices;
using System.Text;
using CSharpKit.Convertions;

using codes_keys_iterator = System.IntPtr;

namespace CSharpKit.Native.ECCODES
{
    // keys_iterator

    /*! \defgroup keys_iterator
    Iterating on keys names
    The keys iterator is designed to get the key names defined in a message.
    Key names on which the iteration is carried out can be filtered through their
    attributes or by the namespace they belong to.
    */
    partial class eccodes
    {
        /// <summary>
        /// 原型: codes_keys_iterator* codes_keys_iterator_new(codes_handle* h, unsigned long filter_flags, const char* name_space) <br/>
        /// 功能:  <br/>
        /// 状态: ok <br/>
        /// </summary>
        /// <param name="handle"></param>
        /// <param name="filter_flags"></param>
        /// <param name="name_space_prt"></param>
        /// <returns> </returns>
        [DllImport(_dllname_, CharSet = CharSet.Auto, EntryPoint = "codes_keys_iterator_new")]
        internal extern static codes_keys_iterator _codes_keys_iterator_new_ptr(IntPtr handle, KeyIteratorFilter filter_flags, IntPtr name_space_prt);
        /// <summary>
        /// 原型: codes_keys_iterator* codes_keys_iterator_new(codes_handle* h, unsigned long filter_flags, const char* name_space) <br/>
        /// 功能: 生成键迭代句柄 <br/>
        /// 状态: ok <br/>
        /// </summary>
        /// <param name="handle"></param>
        /// <param name="filter_flags"></param>
        /// <param name="name_space">e.g. "", "all", "ls", "time", "parameter", "geography", "statistics"</param>
        /// <returns></returns>
        public static codes_keys_iterator codes_keys_iterator_new(IntPtr handle, KeyIteratorFilter filter_flags, string name_space = null)
        {
            IntPtr name_space_ptr = IntPtr.Zero;

            if (!string.IsNullOrEmpty(name_space) && !string.IsNullOrWhiteSpace(name_space))
            {
                var bytes = KConverter.BytesConverter.FromString(name_space);
                name_space_ptr = IntPtrConverter.Instance.FromBytes(bytes);
                if (name_space.ToLower() == "all")
                {
                    name_space_ptr = IntPtr.Zero;
                }
            }

            return _codes_keys_iterator_new_ptr(handle, filter_flags, name_space_ptr);
        }

        /// <summary>
        /// 原型: int codes_keys_iterator_next(codes_keys_iterator* kiter) <br/>
        /// 功能:  <br/>
        /// 状态: ok <br/>
        /// </summary>
        /// <param name="kiter">codes_keys_iterator*</param>
        /// <returns> 0, no </returns>
        [DllImport(_dllname_, CharSet = CharSet.Auto, EntryPoint = "codes_keys_iterator_next")]
        internal extern static int _codes_keys_iterator_next(IntPtr kiter);
        public static bool codes_keys_iterator_next(IntPtr kiter)
        {
            return _codes_keys_iterator_next(kiter) != 0;
        }

        /// <summary>
        /// 原型: const char* codes_keys_iterator_get_name(const codes_keys_iterator* kiter) <br/>
        /// 功能: 获取键名 <br/>
        /// 状态: ok <br/>
        /// </summary>
        /// <param name="kiter">codes_keys_iterator*</param>
        /// <returns> name of key </returns>
        [DllImport(_dllname_, CharSet = CharSet.Auto, EntryPoint = "codes_keys_iterator_get_name")]
        internal extern static IntPtr _codes_keys_iterator_get_name(IntPtr kiter);
        public static string codes_keys_iterator_get_name(IntPtr kiter)
        {
            var p = _codes_keys_iterator_get_name(kiter);
            return IntPtrConverter.Instance.ToString(p);
        }

        /// <summary>
        /// 原型: int codes_keys_iterator_get_string(const codes_keys_iterator* kiter, char* v, size_t* len) <br/>
        /// 功能: 获取键值 <br/>
        /// 状态:  <br/>
        /// </summary>
        /// <param name="kiter"></param>
        /// <param name="msg"></param>
        /// <param name="length"></param>
        /// <returns> </returns>
        [DllImport(_dllname_, CharSet = CharSet.Auto, EntryPoint = "codes_keys_iterator_get_string")]
        internal extern static int codes_keys_iterator_get_string_(IntPtr kiter, byte[] msg, ref int length);
        public static string codes_keys_iterator_get_string(IntPtr kiter)
        {
            var len = BUFFER_SIZE;
            var buffer = new byte[BUFFER_SIZE];
            var ret = codes_keys_iterator_get_string_(kiter, buffer, ref len);
            if (ret != 0) { }  // error
            var key_value = KConverter.BytesConverter.ToString(buffer);

            return key_value;
        }

        /// <summary>
        /// 原型: int codes_keys_iterator_delete(codes_keys_iterator* kiter) <br/>
        /// 功能:  <br/>
        /// 状态: ok <br/>
        /// </summary>
        /// <param name="kiter"></param>
        /// <returns> 0: sucess </returns>
        [DllImport(_dllname_, CharSet = CharSet.Auto, EntryPoint = "codes_keys_iterator_delete")]
        public extern static int codes_keys_iterator_delete(codes_keys_iterator kiter);


        /// <summary>
        /// <para>功能: Rewind the keys iterator.</para>
        /// <para>说明: </para>
        /// </summary>
        /// <param name="kiter">valid codes_keys_iterator </param>
        /// <returns> 0 if OK, integer value on error </returns>
        /// <remarks>
        /// 原型: int codes_keys_iterator_rewind(codes_keys_iterator* kiter);
        /// </remarks>
        [DllImport(_dllname_, CharSet = CharSet.Auto, EntryPoint = "codes_keys_iterator_rewind")]
        public static extern int codes_keys_iterator_rewind(codes_keys_iterator kiter);



        /// <summary>
        /// <para>功能: </para>
        /// <para>说明: </para>
        /// </summary>
        /// <param name="kiter"> </param>
        /// <param name="flags"> </param>
        /// <returns> </returns>
        /// <remarks>
        /// 原型: int codes_keys_iterator_set_flags(codes_keys_iterator* kiter, unsigned long flags)
        /// </remarks>
        [DllImport(_dllname_, CharSet = CharSet.Auto, EntryPoint = "codes_keys_iterator_set_flags")]
        public static extern int codes_keys_iterator_set_flags(codes_keys_iterator kiter, ulong flags);


        /// <summary>
        /// <para>功能: </para>
        /// <para>说明: </para>
        /// </summary>
        /// <param name=""> </param>
        /// <param name=""> </param>
        /// <param name=""> </param>
        /// <returns> </returns>
        /// <remarks>
        /// 原型: int codes_keys_iterator_get_long(const codes_keys_iterator* kiter, long* v, size_t* len);<br/>
        /// </remarks>
        [DllImport(_dllname_, CharSet = CharSet.Auto, EntryPoint = "codes_keys_iterator_get_long")]
        public static extern int codes_keys_iterator_get_long(codes_keys_iterator kiter, long[] v, out int len);

        /// <summary>
        /// <para>功能: </para>
        /// <para>说明: </para>
        /// </summary>
        /// <param name=""> </param>
        /// <param name=""> </param>
        /// <param name=""> </param>
        /// <returns> </returns>
        /// <remarks>
        /// 原型: int codes_keys_iterator_get_double(const codes_keys_iterator* kiter, double* v, size_t* len);
        /// </remarks>
        [DllImport(_dllname_, CharSet = CharSet.Auto, EntryPoint = "codes_keys_iterator_get_double")]
        public static extern int codes_keys_iterator_get_double(codes_keys_iterator kiter, out double[] v, out int len);

        /// <summary>
        /// <para>功能: </para>
        /// <para>说明: </para>
        /// </summary>
        /// <param name=""> </param>
        /// <param name=""> </param>
        /// <param name=""> </param>
        /// <returns> </returns>
        /// <remarks>
        /// 原型: int codes_keys_iterator_get_string(const codes_keys_iterator* kiter, char* v, size_t* len);
        /// </remarks>
        [DllImport(_dllname_, CharSet = CharSet.Auto, EntryPoint = "codes_keys_iterator_get_string")]
        public static extern int codes_keys_iterator_get_string(codes_keys_iterator kiter, out string[] v, out int len);

        /// <summary>
        /// <para>功能: </para>
        /// <para>说明: </para>
        /// </summary>
        /// <param name=""> </param>
        /// <param name=""> </param>
        /// <param name=""> </param>
        /// <returns> </returns>
        /// <remarks>
        /// 原型: int codes_keys_iterator_get_bytes(const codes_keys_iterator* kiter, unsigned char* v, size_t* len);
        /// </remarks>
        [DllImport(_dllname_, CharSet = CharSet.Auto, EntryPoint = "codes_keys_iterator_get_bytes")]
        public static extern int codes_keys_iterator_get_bytes(codes_keys_iterator kiter, out byte[] v, out int len);





        // {{END}}
    }

}

